﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace HQWeb.Resources
{
    public partial class MaterialTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree(LeftTree);
                if (Request["id"] != null)
                {
                    TreeNode node = LeftTree.FindNode(Request["id"]);
                    node.Selected = true;
                    hidName.Value = node.Text;
                    hidID.Value = node.Value;
                }
            }
        }

        #region 展开被选择的地区
        private void ExpendTree()
        {
            TreeNode node = new TreeNode();
            node.Text = "元素分类";
            node.Value = "0";
            node.Expanded = true;
            node.SelectAction = TreeNodeSelectAction.Expand;
            IList TreeList = new ArrayList();
            BLL.SysManage.T_PUB_MATERIALTYPE MaterialBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            TreeList = MaterialBll.GetModelList("ISUSED='1'");
            AddReplies(node, TreeList);
            LeftTree.Nodes.Add(node);
        }
        /// <summary>
        /// 加载父菜单
        /// </summary>
        private void AddExpenNode(TreeNode node, DataTable table)
        {
            DataRow[] rows = table.Select(" F_PARENTID=" + node.Value);

            foreach (DataRow item in rows)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = item["F_REGIONID"].ToString();
                childNode.Text = item["F_REGIONNAME"].ToString();
                childNode.Expand();
                //if (this.hidID.Value.Equals(childNode.Value))
                //{
                //    childNode.Selected = true;
                //}
                node.ChildNodes.Add(childNode);
                AddExpenNode(childNode, table);
            }
        }
        #endregion

        #region 元素加载树形菜单
        public void BindTree(TreeView tree)
        {
            TreeNode node = new TreeNode();
            node.Text = "元素分类";
            node.Value = "0";
            node.Expanded = true;
            //node.SelectAction = TreeNodeSelectAction.Expand;
            IList TreeList = new ArrayList();
            BLL.SysManage.T_PUB_MATERIALTYPE MaterialBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            TreeList = MaterialBll.GetModelList("ISUSED='1'");
            AddReplies(node, TreeList);
            LeftTree.Nodes.Add(node);
        }
        #endregion

        #region 加载元素类型下元素节点
        private void AddReplies(TreeNode node, IList TreeList)
        {
            try
            {
                string strPid = node.Value;
                foreach (Model.SysManage.T_PUB_MATERIALTYPE Material in TreeList)
                {
                    if (Material.PARENT_ID.ToString() == strPid)
                    {
                        TreeNode replyNode = new TreeNode();
                        replyNode.Text = Material.TYPE_NAME.Trim();
                        replyNode.Value = Material.TYPE_ID.ToString();
                        replyNode.Expanded = false;
                        //replyNode.SelectAction = TreeNodeSelectAction.Expand;
                        node.ChildNodes.Add(replyNode);
                        AddReplies(replyNode, TreeList);
                    }

                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 选择节点时选择行业并展开子节点

        protected void LeftTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView tree = (TreeView)sender;
            TreeNode node = tree.SelectedNode;
            hidName.Value = node.Text;
            hidID.Value = node.Value;
            node.Expand();
            TreeNode tempNode = null;
            string strNames = "";
            tempNode = node.Parent;
            if (tempNode == null)
            {

            }
            while (tempNode != null)
            {
                strNames = tempNode.Text + "->" + strNames;
                tempNode = tempNode.Parent;
            }
            this.labSelected.Text = "";
            this.labSelected.Text = strNames + this.hidName.Value;
            hidName.Value = this.labSelected.Text;
        }
        #endregion
    }
}